package com.example.yyy

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import org.jetbrains.anko.AnkoAsyncContext
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.doAsyncResult
import org.jetbrains.anko.uiThread
import java.util.concurrent.Future

class ThreadActivity : AppCompatActivity() {

    companion object val UPDATE_UI = 1
    private val handle = object:Handler(){
        override fun handleMessage(msg: Message) {
            when(msg.what){
                UPDATE_UI -> Log.e("TAG", "handleMessage: 11111 " )
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_thread)
        PlayThread().start()
        Thread{
            val message = Message()
            message.what = UPDATE_UI
            handle.sendMessage(message)
        }.start()
        //牵扯到支线程交互的线程
        doAsync {

            //回主线程
            uiThread {

            }
        }
        //支线程返回值
        val longTask:AnkoAsyncContext<Context>.() -> String = {
            for (r in 0..20)Thread.sleep(200)
            "事件处理完成"
        }
        val future:Future<String> = doAsyncResult(null,longTask)
        for (c in 0..10){
            if (future.isDone){

            }
        }
    }
    private inner class PlayThread :Thread(){
        override fun run() {

        }
    }
}